<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售线索编号" prop="saleClueNo">
        <el-input
          v-model="queryParams.saleClueNo"
          placeholder="请输入销售线索编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人姓名" prop="telName">
        <el-input
          v-model="queryParams.telName"
          placeholder="请输入联系人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商机来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入商机来源"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入省份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入城市"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="staffFollower">
        <el-input
          v-model="queryParams.staffFollower"
          placeholder="请输入负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="  mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:source:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:source:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:source:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:source:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sourceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" class-name="small-padding fixed-width" prop="customerName">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="getCsCustomer(scope.row)"
                  v-hasPermi="['crm:cscustomer:list']"
                >{{scope.row.customerName}}</el-button>
              </template>
      </el-table-column>
      <el-table-column label="销售线索编号" align="center" prop="saleClueNo" />
      <el-table-column label="联系人姓名" align="center" prop="telName" />
      <el-table-column label="手机号码" align="center" prop="telephone" />
      <el-table-column label="商机来源" align="center" prop="source" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="负责人" align="center" prop="staffFollower" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:source:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:source:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="销售线索编号" prop="saleClueNo">
          <el-input v-model="form.saleClueNo" placeholder="请输入销售线索编号" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="telName">
          <el-input v-model="form.telName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="手机号码" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="商机来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入商机来源" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-input v-model="form.province" placeholder="请输入省份" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="负责人" prop="staffFollower">
          <el-input v-model="form.staffFollower" placeholder="请输入负责人" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  <!-- 销售线索客户的详细信息 -->
<el-dialog :title="title" :visible.sync="dialogVisible" width="800px" append-to-body>
  <el-tabs v-model="activeName" @tab-click="handleClick">
    <el-tab-pane label="详情" name="first">
    <el-collapse v-model="activeNames" @change="handleChange">
      <el-collapse-item title="基本信息" name="1">
     <el-form ref="CsCustomer" :model="CsCustomer" :rules="rules" label-width="80px">
      <el-descriptions class="margin-top"  :column="2" :size="size" border>
        <el-descriptions-item >
          <template slot="label">
                客户名称
          </template>
          {{customerSource.customerName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
                销售线索编号
          </template>
          {{CsCustomer.saleClueNo}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
                联系人姓名
          </template>
          {{customerSource.telName}}
        </el-descriptions-item>
        <el-descriptions-item>
           <template slot="label">
                手机号码
           </template>
           {{customerSource.telephone}}
        </el-descriptions-item>
        <el-descriptions-item>
           <template slot="label">
                商机来源
           </template>
           {{customerSource.source}}
        </el-descriptions-item>
        <el-descriptions-item>
           <template slot="label">
                负责人
           </template>
           {{customerSource.staffFollower}}
        </el-descriptions-item>
        <el-descriptions-item>
           <template slot="label">
                创建时间
           </template>
           {{CsCustomer.createTime}}
        </el-descriptions-item>
        <el-descriptions-item>
           <template slot="label">
                创建人
           </template>
           {{CsCustomer.creator}}
        </el-descriptions-item>
        <el-descriptions-item>
           <template slot="label">
                修改时间
           </template>
           {{customerSource.updateTime}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
                最新更新人
          </template>
          {{CsCustomer.updatePerson}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
                关闭原因
          </template>
          {{CsCustomer.loseReason}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
                最新联系日期
          </template>
          {{CsCustomer.contactDateLast}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
                最新进展
          </template>
         {{CsCustomer.lastProgress}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
               未联系天数
          </template>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
                分配日期
          </template>
          {{CsCustomer.assignDate}}
        </el-descriptions-item>
        <el-descriptions-item >
          <template slot="label">
                商机状态
          </template>
          {{CsCustomer.status}}
        </el-descriptions-item>
      </el-descriptions>
     </el-form>
     </el-collapse-item>
      <el-collapse-item title="地址信息" name="2">
      <el-form ref="CsCustomer" :model="CsCustomer" :rules="rules" label-width="80px">
        <el-descriptions class="margin-top"  :column="2" :size="size" border>
              <el-descriptions-item >
                <template slot="label">
                      国家
                </template>
                {{CsCustomer.country}}
              </el-descriptions-item>
              <el-descriptions-item >
                <template slot="label">
                      省份
                </template>
                {{customerSource.province}}
              </el-descriptions-item>
              <el-descriptions-item >
                <template slot="label">
                      城市
                </template>
                {{customerSource.city}}
              </el-descriptions-item>
              <el-descriptions-item >
                <template slot="label">
                      区域
                </template>
                {{CsCustomer.region}}
              </el-descriptions-item>
              <el-descriptions-item >
                <template slot="label">
                      地址
                </template>
                {{CsCustomer.address}}
              </el-descriptions-item>
        </el-descriptions>
     </el-form>
      </el-collapse-item>
     </el-collapse-item>
      <el-collapse-item title="备注信息" name="3">
        <el-form ref="CsCustomer" :model="CsCustomer" :rules="rules" label-width="80px">
          <el-descriptions class="margin-top"  :column="2" :size="size" border>
                  <el-descriptions-item >
                    <template slot="label">
                          备注
                    </template>
                    {{CsCustomer.remark}}
                  </el-descriptions-item>
          </el-descriptions>
        </el-form>
      </el-collapse-item>
     </el-collapse-item>
      <el-collapse-item title="评论信息" name="4">
        <el-form ref="CsCustomer" :model="CsCustomer" :rules="rules" label-width="80px">
          <el-descriptions class="margin-top"  :column="2" :size="size" border>
                  <el-descriptions-item >
                    <template slot="label">
                          评论
                    </template>
                    {{CsCustomer.comment}}
                  </el-descriptions-item>
          </el-descriptions>
        </el-form>
      </el-collapse-item>
     </el-collapse>
    </el-tab-pane>

    <el-tab-pane label="跟进记录" name="followRecord">
         <el-form ref="FollowRecord" :model="FollowRecord" :rules="rules" label-width="100px">
                        <el-table :data="FollowRecord" @selection-change="handleSelectionChange" empty-text="暂无数据">
                          <el-table-column label="主题" align="center" prop="theme"/>
                          <el-table-column label="客户名称" align="center" prop="customerName" />
                          <el-table-column label="联系人姓名" align="center" prop="telName" />
                          <el-table-column label="客户状态" align="center" prop="status" />
                          <el-table-column label="联系方式" align="center" prop="howContact" />
                          <el-table-column label="联系日期" align="center" prop="contactDate" />
                          <el-table-column label="负责人" align="center" prop="staffFollower" />
                          <el-table-column label="跟进记录" align="center" prop="followRecord" />
                          <el-table-column label="最新修改时间" align="center" prop="updateTime" />
                        </el-table>
                      </el-form>
    </el-tab-pane>
  </el-tabs>
   <div slot="footer" class="dialog-footer">
     <el-button type="primary" @click="submitForm1">确 定</el-button>
     <el-button @click="cancel1">取 消</el-button>
   </div>
</el-dialog>
  </div>
</template>

<script>
import { listSource, getSource, delSource, addSource, updateSource ,getCsCustomer,getFollowRecord} from "@/api/crm/source";

export default {
  name: "Source",
  data() {
    return {
      customerSource: {},
      activeNames: ['1'],
      activeName: 'second',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      size:'',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      sourceList: [],
      CsCustomer: {},
      FollowRecord: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        saleClueNo: null,
        telName: null,
        telephone: null,
        source: null,
        province: null,
        city: null,
        staffFollower: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listSource(this.queryParams).then(response => {
        this.sourceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCsCustomer(row){
      const customerName = row.customerName;
      getCsCustomer(customerName).then(response => {
          this.CsCustomer=response;
          this.customerSource = response.customerSource
          this.dialogVisible=true;
          this.title= "销售客户";
          this.size='50';
      })
      getFollowRecord(customerName).then(response => {
                          let list=response.data;
                          this.FollowRecord.push(...response.data);
                          console.log(this.FollowRecord)
                          this.dialogVisible=true;
                          this.size='50';
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
        cancel1() {
          this.dialogVisible = false;
          this.reset();
        },
         handleChange(val) {
              },
     handleClick(tab, event) {
          },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customerName: null,
        saleClueNo: null,
        telName: null,
        telephone: null,
        source: null,
        province: null,
        city: null,
        staffFollower: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSource(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSource(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSource(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
        submitForm1() {
          this.dialogVisible = false;
          this.reset();
        },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSource(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/source/export', {
        ...this.queryParams
      }, `system_source.xlsx`)
    }
  }
};
</script>
